package com.zoshine.zzg.service;

import com.zoshine.zzg.config.ProjectConfig;
import com.zoshine.zzg.dao.*;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.CommonStatus;
import com.zoshine.zzg.pojo.table.Menus;
import com.zoshine.zzg.pojo.table.SystemParams;
import com.zoshine.zzg.pojo.table.Tables;
import com.zoshine.zzg.pojo.table.base.Column;
import com.zoshine.zzg.pojo.table.base.Table;
import com.zoshine.zzg.util.idworker.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 表结构及数据初始化服务
 * @author 贾俊峰
 * @time 2017/8/10 14:39
 */
@Service
public class InitService {
    @Resource
    private IdWorker<Long> baseIdWorker;
    @Autowired
    private InitDao initDao;
    @Autowired
    private SystemParamDao systemParamDao;
    @Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private MenuDao menuDao;

    /**
     * 判断系统是否可以进行初始化操作
     */
    public boolean initAble() {
        boolean hasTableSystemParam = initDao.hasTable("system_param");
        if (!hasTableSystemParam) {
            return true;
        }
        SystemParam systemParam = new SystemParam();
        systemParam.setParamName("initAble");
        systemParam = systemParamDao.selectOne(systemParam);
        return systemParam == null || Objects.equals(systemParam.getParamValue(), "true");
    }

    /**
     * 表结构初始化
     */
    public void initTable() {
        if (isInner()) {
            // 内网
            for (Table table:Tables.INNER_TABLE_LIST) {
                if (initDao.hasTable(table.getTableName())) {
                    initDao.dropTable(table);
                }
                initDao.createTable(table);
                initDao.alterTableComment(table);
                for (Column column:table.getColumns()) {
                    initDao.alterColumnComment(table.getTableName(), column);
                }
            }
        } else if (isOuter()) {
            // 外网
            for (Table table:Tables.OUTER_TABLE_LIST) {
                if (initDao.hasTable(table.getTableName())) {
                    initDao.dropTable(table);
                }
                initDao.createTable(table);
            }
        }
    }

    /**
     * 数据初始始化
     */
    public void initData() {
        Date now = new Date();

        // 组织信息初始化
        initDao.truncateTable("organization");
        Organization organization = new Organization();
        organization.setId(0L);
        organization.setOrgName("浙江卓想科技有限公司");
        organization.setOrgType(0);
        organization.setOrgStatus(CommonStatus.VALID.getValue());
        organization.setBankName("中国农业银行");
        organization.setBankCard(60000000000000L);
        organization.setBalance(0);
        organization.setCreateTime(now);
        organization.setUpdateTime(now);
        organizationDao.insert(organization);

        // 角色信息初始化
        initDao.truncateTable("role");
        Role role = new Role();
        role.setId(0L);
        role.setOrgId(organization.getId());
        role.setRoleName("管理员");
        role.setRoleStatus(CommonStatus.VALID.getValue());
        role.setCreateTime(now);
        role.setUpdateTime(now);
        roleDao.insert(role);

        // 管理员初始化
        initDao.truncateTable("user_member");
        User user = new User();
        user.setId(0L);
        user.setUsername("admin");
        user.setSalt("123456");
        user.setPassword("5f1d7a84db00d2fce00b31a7fc73224f");
        user.setUserStatus(CommonStatus.VALID.getValue());
        user.setCreateTime(now);
        user.setUpdateTime(now);
        user.setOrgId(organization.getId());
        user.setRoleId(role.getId());
        user.setRealName("管理员");
        userDao.insert(user);

        // 系统参数初始化
        initDao.truncateTable("system_param");
        for (SystemParam systemParam : SystemParams.SYSTEM_PARAM_LIST) {
            systemParam.setId(baseIdWorker.createId());
            systemParam.setCreateTime(now);
            systemParam.setUpdateTime(now);
            systemParam.setCreateUser(user.getId());
            systemParam.setUpdateUser(user.getId());
            systemParamDao.insert(systemParam);
        }

        // 菜单初始化
        initDao.truncateTable("menu");
        if (isInner()) {
            for (Menu menu: Menus.INNER_MENU_LIST) {
                menu.setId(baseIdWorker.createId());
                menu.setCreateTime(now);
                menu.setUpdateTime(now);
                menuDao.insert(menu);
            }
        } else if (isOuter()) {
            for (Menu menu: Menus.OUTER_MENU_LIST) {
                menu.setId(baseIdWorker.createId());
                menu.setCreateTime(now);
                menu.setUpdateTime(now);
                menuDao.insert(menu);
            }
        }
    }

    private boolean isInner() {
        return Objects.equals(projectConfig.getEnvironment().toLowerCase(), "inner");
    }

    private boolean isOuter() {
        return Objects.equals(projectConfig.getEnvironment().toLowerCase(), "outer");
    }
}
